var app = getApp(), fetchApi = require("../../../utils/httpPost.js"), 
i = require("../../../utils/arrayUtil.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    sections:[],
    pageSize:10,
    pageIndex:1,
    keyword:'',
    isLoading:'true',
    partner_type:0,
    sale_type:1,//  0 -进货商品  1 - 普通自有商品
    productList:[],
    productTypes:[],
    productTypeIds:[],
    currentTab:0,
    type_id:0,
    type_child_id:0,
    typeName:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      console.log('商品列表 options',options)
    if(options.sale_type){
      this.data.sale_type = parseInt(options.sale_type)
    }
    if(options.type){
        this.data.type_id = parseInt(options.type);
    }
    if(options.type_child_id){
        this.data.type_child_id = parseInt(options.type_child_id);
    }
    this.onHttpInit()
  },
  handleChange({detail}) {
    console.log('handleChange',detail)
    this.data.currentTab = detail.index;
    this.data.productList = [];
    this.setData({
      currentTab: detail.index,
      productList:[],
      orderList:[],
    });
    this.data.pageIndex = 1;
    this.data.type_id = this.data.productTypeIds[detail.index];
    this.onHttpInit()
  },
  onChange(e){
    console.log('onChange',e)
    this.data.keyword = e.detail.value
    this.setData({
        keyword: e.detail.value
    });
  },
  onSearch(val){
    console.log('onSearch',val)
    this.data.pageIndex = 1
    this.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      productList:[],
    })
    this.onHttpInit()
  },
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost("/product/whole/list", {
      keyword:that.data.keyword,
      pageSize:that.data.pageSize,
      pageIndex: that.data.pageIndex,
      type_id:that.data.type_id,
      type_child_id: that.data.type_child_id,
    }, function (res) {
      console.log("获取商品列表返回数据:", res);
      if(res.data.data.typeName!=undefined){
          wx.setNavigationBarTitle({
            title: res.data.data.typeName,
          })
      }

      if(res.data.totalcount > 0){
        that.data.pageIndex = that.data.pageIndex + 1

        that.setData({ 
          productTypes:res.data.data.productTypes,
        });
        var products = res.data.data.products;
        if(products.length > 0){
          for (var index in products) {
            that.data.productList.splice(that.data.productList.length, 0, products[index]);
          }
          console.log('productList',that.data.productList)
          that.setData({ 
            productList: that.data.productList
          });
        }
      }
      that.setData({ isLoading: false, loading: true, netWork: true })
      wx.hideNavigationBarLoading() //完成停止加载
      // complete
      wx.stopPullDownRefresh() //停止下拉刷新
    }, function (a) {
      console.log("获取商品列表返回数据 err:", a);
    });
  },
  searchScrollLower(){
    this.onHttpInit()
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.pageIndex = 1
    this.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      productList:[],
    })
    this.onHttpInit()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.onHttpInit()
  },
})